/**
 * This class implements appropriate methods to 
 * display chart of the FrequencyDistribution taking data from FrequencyDistributionDataProvider 
 */
package org.wmine.visualization;

import java.awt.Color;
import java.awt.GradientPaint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryMarker;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.wmine.visualization.data.AbstractChartDataProvider;
import org.wmine.visualization.data.FrequencyDistributionData;
import org.wmine.visualization.data.FrequencyDistributionDataProvider;
import org.wmine.visualization.data.FrequencyDistributionData.FreqDistDataSingleClass;
import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur 
 */
public class DrawFrequencyDistributionGraph  implements  IDrawMiningAlgorithmOutput{

	AbstractChartDataProvider freq;
	FrequencyDistributionData frqData;
	static DrawFrequencyDistributionGraph unicInstance;
	double[]  TempValues;
	ArrayList<java.lang.Double>	Yvalues;
	ArrayList<java.lang.Double>	Xvalues;
	String outputFileName;
	public DrawFrequencyDistributionGraph(String outputFileName) {
		this.outputFileName = outputFileName;
		Xvalues = new ArrayList<Double>();
		Yvalues = new ArrayList<Double>();
		
		// TODO Auto-generated constructor stub
	}
	
	public SetChartPanel drawGraph() throws ParserConfigurationException, SAXException, IOException {
		// TODO Auto-generated method stub
		JFreeChart chart;
		//System.out.println(outputFileName+"to test");
		
		try {
			freq = new FrequencyDistributionDataProvider(outputFileName);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<FreqDistDataSingleClass> classList = new ArrayList<FreqDistDataSingleClass>();
		frqData = (FrequencyDistributionData) freq.getChartData();
		classList = frqData.getClassList();
		//System.out.println(classList.size()+"Size of classes" );
		
		for (int i = 0; i < classList.size(); i++) {
			FreqDistDataSingleClass element = frqData.getFreqDistDataSingleClassInstance();
			element =(FreqDistDataSingleClass) classList.get(i);
			//System.out.println(element.getLowerBound());
			Xvalues.add(element.getLowerBound());
			Yvalues.add(element.getFrequency());
		}
		TempValues = new double[Yvalues.size()];
		int i =0 ;
		for (Iterator iter = Yvalues.iterator(); iter.hasNext();) {
			Double element = (Double) iter.next(); 
			TempValues[i] = element.doubleValue();
			i++;			
		}
		chart = setupChartProperties();
		SetChartPanel setChart= new SetChartPanel(chart);
		//System.out.println("In frequency drawgraph method");
		return setChart;
	}
	private JFreeChart setupChartProperties() {
		// TODO Auto-generated method stub
		JFreeChart chart1;
		DefaultCategoryDataset dataset = createDataset();
		
		chart1 = createChart(dataset);
		return chart1;
	}

	private JFreeChart createChart(CategoryDataset dataset) {
		// TODO Auto-generated method stub
		  final JFreeChart chart = ChartFactory.createBarChart(
		            "Frequency Distribution",      // chart title
		            "Intervals",               // domain axis label
		            "Frequency",                  // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips
		            false                     // urls
		        );

		        final CategoryPlot plot = chart.getCategoryPlot();
		        
		        final CategoryAxis axis = plot.getDomainAxis();
		       // System.out.println("Test "+plot.getAnnotations());
		        CategoryMarker tempMarker = new CategoryMarker(null);
		        /*for (Iterator iter = Xvalues.iterator(); iter.hasNext();) {
					java.lang.Double element = (java.lang.Double) iter.next();
					tempMarker.setLabel(element.toString());	
				}
		        plot.addDomainMarker(tempMarker);*/
		        
		        axis.setCategoryLabelPositions(
		            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 15.0)
		        );
		        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
		        renderer.setDrawBarOutline(false);
		        GradientPaint gp2 = new GradientPaint(
		        		0.0f, 0.0f, Color.red,
		        		0.0f, 0.0f, Color.lightGray
		        		);
		        		renderer.setSeriesPaint(0, gp2);

		        return chart;
		 }

	private DefaultCategoryDataset createDataset() {
		// TODO Auto-generated method stub
		String series1 = "First";
		DefaultCategoryDataset data = new DefaultCategoryDataset();
		for (int i = 0; i < TempValues.length; i++) {
			
			data.addValue(TempValues[i], series1, Xvalues.get(i).toString());
		}
		return data;	 

	}
	
	

  
}
